package me.sdevil507.org.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.org.dto.OrgUserLoginParamsDto;
import me.sdevil507.org.dto.OrgUserSmsLoginParamsDTO;
import me.sdevil507.org.events.OrgUserLastLoginTimeEvent;
import me.sdevil507.org.po.OrgUserPo;
import me.sdevil507.org.service.OrgUserService;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.enums.LoginChannel;
import me.sdevil507.supports.shiro.enums.LoginModeType;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.supports.shiro.kickout.ShiroLoginProxy;
import me.sdevil507.supports.shiro.realm.OrgUsernamePasswordRealm;
import me.sdevil507.supports.shiro.token.SmsToken;
import me.sdevil507.supports.shiro.token.UsernamePasswordToken;
import me.sdevil507.supports.status.ApiStatusCode;
import me.sdevil507.systemAssist.Constant.SystemAssistConstant;
import me.sdevil507.systemAssist.systemLog.annotation.OperLog;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

/**
 * 机构端用户Controller操作
 *
 * @author sdevil507
 * created on 2019/12/15
 */
@RestController
@RequestMapping("/api/expert/user")
@Slf4j
public class ExpertUserLoginController {

    @Autowired
    private ShiroLoginProxy shiroLoginProxy;

    @Autowired
    private ApplicationEventPublisher publisher;

    @Autowired
    private OrgUserService orgUserService;

    @Autowired
    private OrgUsernamePasswordRealm orgUsernamePasswordRealm;

    /**
     * 手机号/密码登录
     *
     * @param dto 入参dto
     * @return 执行反馈
     */
    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.EXPERT, operClassify = SystemAssistConstant.systemAssistClassify.SECURITY, operModule = SystemAssistConstant.systemAssistModule.LOGIN, operDesc = "密码登录", recordRequestParams = false)
    @PostMapping("/login")
    public ApiResultDTO login(@RequestBody @Valid OrgUserLoginParamsDto dto) {
        OrgUserPo orgUserPo = orgUserService.readOneByPhoneNumber(dto.getPhoneNumber());
        // 判断是否有专家角色
        if (null == orgUserPo || !orgUserService.containsExpert(orgUserPo.getId())) {
            // 没该用户或没角色
            return ApiResultGenerator.create(ApiStatusCode.ACCOUNT_INVALID.getCode(), ApiStatusCode.ACCOUNT_INVALID.getDescription());
        }
        if(orgUserPo.getLocked()){
            return ApiResultGenerator.create(ApiStatusCode.ACCOUNT_LOCKED.getCode(), ApiStatusCode.ACCOUNT_LOCKED.getDescription());
        }
        // 专家用户
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(dto.getPhoneNumber(), dto.getPassword(), LoginChannel.ORG, LoginModeType.USERNAME_PASSWORD);
        shiroLoginProxy.login(usernamePasswordToken);
        String token = SecurityUtils.getSubject().getSession().getId().toString();
        ApiResultDTO apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), token);
        // 发布更新最后登录时间事件
        publisher.publishEvent(new OrgUserLastLoginTimeEvent(this, OrgAccountHelper.getUsername(), new Date()));
        OrgAccountHelper.setUserId(orgUserPo.getId());
        // 清除缓存
        orgUsernamePasswordRealm.clearCachedInfo(SecurityUtils.getSubject().getPrincipal().toString());
        return apiResultDTO;
    }


}
